package studentManage.B_Controller;

import studentManage.C_Service.StudentService;
import studentManage.E_Domain.Student;

import java.util.Scanner;

public abstract class baseStudentController {
    private StudentService studentService = new StudentService();
    private Scanner sc = new Scanner(System.in);

    //开启学生管理系统并且展示学生管理系统菜单
    public void start() {
        studentLoop:
        while (true) {
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    System.out.println("添加学生");
                    addStudent();
                    break;
                case "2":
                    System.out.println("删除学生");
                    deleteStudentById();
                    break;
                case "3":
                    System.out.println("修改学生");
                    updateStudent();
                    break;
                case "4":
                    System.out.println("查看学生");
                    findAllStudent();
                    break;
                case "5":
                    System.out.println("感谢使用学生系统");
                    break studentLoop;
                default:
                    System.out.println("您的输入有误，请重新输入");
            }

        }
    }

    //删除学生方法
    public void deleteStudentById() {
        String delId = inputStudentId();
        studentService.deleteStudentById(delId);
        System.out.println("删除成功");

    }

    //查找学生方法
    public void findAllStudent() {

        Student[] stus = studentService.findAllStudent();
        if (stus == null) {
            System.out.println("查无信息，请添加后重试");
            return;
        }
        System.out.println("学号\t\t姓名\t年龄\t生日");
        for (int i = 0; i < stus.length; i++) {
            Student stu = stus[i];
            if (stu != null) {
                System.out.println(stu.getId() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t\t" + stu.getBirthday());
            }
        }


    }

    //修改学生方法
    public void updateStudent() {
        Student[] stus = studentService.findAllStudent();
        if (stus == null) {
            System.out.println("查无信息，请添加后重试");
            return;
        }
        String updateId = inputStudentId();
        Student newStu = inputStudentInfo(updateId);
        studentService.updateStudent(updateId, newStu);
        System.out.println("修改成功");

    }

    //添加学生方法
    public void addStudent() {
        String id;
        StudentService studentService = new StudentService();
        //1.键盘接收学生信息
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入学生id");
            id = sc.next();

            boolean flag = studentService.isExists(id);
            if (flag == true) {
                System.out.println("id已经被使用");
            } else {
                break;
            }
        }

        Student stu = inputStudentInfo(id);
        //将学生对象传递给StudentService中的addStudent方法

        boolean result = studentService.addStudent(stu);
        if (result) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
    }

    //键盘录入学生id
    public String inputStudentId() {
        String id;
        while (true) {
            System.out.println("请输入学生的Id");
            id = sc.next();
            boolean exists = studentService.isExists(id);
            if (!exists) {
                System.out.println("您输入的id不存在，请重新输入：");
            } else {
                break;
            }
        }
        return id;

    }
    public abstract Student inputStudentInfo(String id) ;

}
